package org.jeecg.modules.simulator.service;



import lombok.Setter;
import org.jeecg.modules.simulator.jsonot.BeforeFiveMinPeople;
import org.jeecg.modules.simulator.model.SceneDataType;
import org.jeecg.modules.simulator.model.SceneParam;
import org.jeecg.modules.simulator.model.train.event.TrainActionListenRecorder;
import org.jeecg.modules.simulator.model.train.event.TrainListener;
import org.jeecg.modules.simulator.recorder.StationInfoVo;
import org.jeecg.modules.simulator.rooter.CaculateProject;


import org.jeecg.modules.simulator.util.Random16StringGenerator;
import org.jeecg.modules.simulator.util.TimeUtil;
import org.jeecg.modules.simulator.util.UnitCache;
import org.jeecg.modules.simulator.vo.LineStaticInfoVo;
import org.jeecg.modules.simulator.vo.ProjectControlStatus;
import org.jeecg.modules.simulator.vo.SceneControlStatus;
import org.jeecg.modules.simulator.vo.StationStaticInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * @program: gp
 * @description:
 * @author: lydms
 * @create: 2024-08-07 14:00
 **/
@Service
public class GodMapService {
    @Autowired
    TrainListener trainListener;//车辆事件监听，放在这里是因为很多对象这个级别可以放在一起
    //根据上车人数决定下车的困难度，人数越多下车越困难

//    @Autowired
//    T80RouteHisService t80RouteHisService;

    @Autowired
    HistoryBizTrainLoaderService historyBizTrainLoaderService;
    @Autowired
    HistoryBizPersonLoaderService historyBizPersonLoaderService;
    @Autowired
    TrainActionListenRecorder trainActionListenRecorder;



    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    QueryPasgrPeriodServiceImpl queryPasgrPeriodService;

/**
 * @description: 创建项目
 *
 * @author:  renzhg
 * @date:  2024/11/12 9:35
 **/
    public CaculateProject createProject( SceneParam sceneParam, Integer step,RedisTemplate<String, Object> redisTemplate) {
        System.out.println("createProject");
        this.redisTemplate=redisTemplate;
//        String historyStartTime="07:00:00";
//        String historyEndTime="09:00:00";
//        String historyDate="2024-10-01";

//        Integer step = 1000;
        String sceneCode=Random16StringGenerator.generateRandomString();
        UnitCache.sceneParamMap.put(sceneCode,sceneParam);
        SceneControlStatus sceneControlStatus =new SceneControlStatus();
        sceneControlStatus.setProjectControlStatus(ProjectControlStatus.start);
        sceneControlStatus.setStep(step);
        UnitCache.sceneControlStatusMap.put(sceneCode,sceneControlStatus);
        CaculateProject caculateProject = new CaculateProject(sceneCode,trainListener,redisTemplate);
        caculateProject.setHistoryBizPersonLoaderService(historyBizPersonLoaderService);
        caculateProject.setHistoryBizTrainLoaderService(historyBizTrainLoaderService);
        caculateProject.setQueryPasgrPeriodService(queryPasgrPeriodService);
        caculateProject.setRedisTemplate(this.redisTemplate);
        initTimeString(sceneParam.getStartTime(),sceneParam.getEndTime(),sceneCode);
        caculateProject.initProjectParam();
        caculateProject.projectInit();

        Map<String, StationStaticInfoVo> stringStationStaticInfoVoMap = caculateProject.afeterInitGetStationInfo();
        UnitCache.sceneControlMap.put(sceneCode,caculateProject);
        UnitCache.stationStaticInfoVoMap.put(sceneCode,stringStationStaticInfoVoMap);
        StationInfoVo stationInfoVo = new StationInfoVo();

        UnitCache.stationInfoVoMap.put(sceneCode,stationInfoVo);
        LineStaticInfoVo lineStaticInfoVo = caculateProject.afterInitGetLineStaticInfo();
        UnitCache.lineStaticInfoVoMap.put(sceneCode,lineStaticInfoVo);
//        caculateProject.projectRun();

//        Thread thread1 = new Thread(caculateProject);
//        caculateProject.run();


        Future<?> future = UnitCache.executorService.submit(caculateProject);
        UnitCache.sceneFutureMap.put(sceneCode,future);



//        thread1.start();

        return caculateProject;
    }
    public void initTimeString(LocalTime startTime, LocalTime endTime,String sceneCode){
        List<String> timeString = TimeUtil.generateTimeSlots(startTime, endTime);
        Map<String,List<BeforeFiveMinPeople>> beforeFiveMinPeopleMap = new HashMap<>();
        for (int i = 0; i < timeString.size(); i++) {
            List<BeforeFiveMinPeople> list=new ArrayList<>();
            beforeFiveMinPeopleMap.put(timeString.get(i),list);
        }
        UnitCache.BeforeFiveMinPeopleMap.put(sceneCode,beforeFiveMinPeopleMap);

    }







}
